package com.yd.mvc.pc.shareController;

import com.yd.app.service.common.LoggerService;
import com.yd.app.service.pc.shareSrevice.ShareService;
import com.yd.domain.business.Share;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.ShareParam;
import com.yd.domain.sys.Logger;
import com.yd.mvc.baseController.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zy
 * @create 2018-03-12 9:40
 * @desc
 **/
@RestController
@RequestMapping(value = "/api/share")
public class ShareController extends BaseController {
    @Autowired
    private ShareService shareService;
    @Autowired
    private LoggerService loggerService;
    /**
     * @Author: zy
     * @Desc: 录入分享信息
     * @Date:  2018/3/12
     * @Param
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addShare(@RequestBody Share s,HttpServletRequest request)  {
        String user="admin";
        String suff="发布人:"+user+";状态:"+s.getStatus()+";标题:"+s.getTitle()+";时间:"+s.getPublishTime()+";商品:"+s.getGoodId();
        try {
            s.setPublishUser(user);//录入人
            shareService.addShare(s);
            loggerService.addLogger(new Logger("新增分享信息信息",user,Logger.LOG_TYPE.ADD,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.SHAREINFO,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("新增分享信息信息",user,Logger.LOG_TYPE.ADD,Logger.STATUS.FAIL,Logger.MODEL_TYPE.SHAREINFO,"",suff),request);
            e.printStackTrace();
        }


    }
    /**
     * @Author: zy
     * @Desc: 修改
     * @Date:  2018/3/12
     * @Param
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public void updateShare(@RequestBody Share share,HttpServletRequest request){
        String user="admin";
        String suff="ID:"+share.getId()+";发布人:"+user+";状态:"+share.getStatus()+";标题:"+share.getTitle()+";时间:"+share.getPublishTime()+";商品:"+share.getGoodId();
        try {
            shareService.updateShare(share);
            loggerService.addLogger(new Logger("修改分享信息信息",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.SHAREINFO,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("修改分享信息信息",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.SHAREINFO,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 删除
     * @Date: 2018/3/12
     * @Param
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public void deleteShare(@PathVariable("id") String id,HttpServletRequest request){
        String user="admin";
        String suff="ID:"+id;
        try {
            shareService.deleteShare(id);
            loggerService.addLogger(new Logger("删除分享信息信息",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.SHAREINFO,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("删除分享信息信息",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.SHAREINFO,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 审核
     * @Date:  2018/3/12
     * @Param
     */
    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    public void verifyShare(@RequestBody Share share,HttpServletRequest request){
        String user="admin";
        String suff="状态:"+share.getStatus()+";ID:"+share.getId();
        try {
            shareService.verifyShare(share);
            loggerService.addLogger(new Logger("审核分享信息信息",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.SHAREINFO,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("审核分享信息信息",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.SHAREINFO,"",suff),request);
            e.printStackTrace();
        }

    }
    /**
     * @Author: zy
     * @Desc: 分页查询数据列表
     * @Date:  2018/3/12
     * @Param
     */
    @RequestMapping(value = "/list/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getShareList(@RequestBody ShareParam shareParam, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize){
        PageModel pm=shareService.getShareList(shareParam,pageIndex,pageSize);
        return pm;
    }
}
